import requests
import execjs
import json
# 导入库
from openpyxl import Workbook
# 导入Font、PatternFill、Alignment类
from openpyxl.styles import Font,PatternFill,Alignment



cookies = {
    'gr_user_id': '907c294f-a17d-4b79-bdb9-3a2db0331d0b',
}

headers = {
    'Accept': 'text/plain, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    # 'Cookie': 'gr_user_id=907c294f-a17d-4b79-bdb9-3a2db0331d0b',
    'Origin': 'https://www.endata.com.cn',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

data = {
    'sdate1': '2023-10-06',
    'edate1': '2023-10-08',
    'sdate2': '2023-09-29',
    'edate2': '2023-10-01',
    'MethodName': 'BoxOffice_GetWeekendInfoData',
}

response = requests.post('https://www.endata.com.cn/API/GetData.ashx', cookies=cookies, headers=headers, data=data)

js_file = open("混淆js.js","r",encoding="utf-8").read()
datas = execjs.compile(js_file).call("webInstace.shell",response.text)



# 创建 Workbook 工作表
wb = Workbook()
# 设置当前活动工作表
ws = wb.active

# 设置行标题
row_title = ['MovieRank','MovieID','MovieName','BoxOffice','SumBoxOffice','AvgPrice','AvpPeoPle','MovieDay','DefaultImage','Rank_Up','BoxOffice_Up','RapIndex']
# 使用 enumerate() 函数将一个可迭代对象转换为一个枚举对象，并设置起始索引为1
for col_num,title in enumerate(row_title,1):
    # 设置单元格的值
    # row和colum分别的单元格的行号和列号。value是单元格的内容
    cell = ws.cell(row=1,column=col_num,value=title)
    # 一次性设置字体名称、字体加粗、字体大小、字体颜色
    font = Font(name='楷体',bold=True,size=18,color='ffffff')
    cell.font = font

    # 设置单元格填充样式
    fill = PatternFill(fill_type='solid',start_color='596dc8')
    cell.fill = fill

    # 设置单元格对齐方式:水平居中、垂直居中
    alignment = Alignment(horizontal='center',vertical='center')
    cell.alignment = alignment
    #indent=10,wrap_text=True,textRotation=45
# 遍历数据，
for i,item in enumerate(json.loads(datas)['Data']['Table']):
    # 使用列表推导式：遍历行标题，将表格对应的单元格数据放入数组中，如果没有设置为 ''
    row_data = [item.get(row,'') for row in row_title]
    # 写入工作簿
    ws.append(row_data)

# 保存工作簿
wb.save('data.xlsx')
